<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="dashboard.User"%>
<%@page import="dashboard.UserRegistry" %>
<%@page import="dashboard.CourseRegistry"%>
<%@page import="dashboard.Activity" %>
<%@page import="dashboard.Course"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Date"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
<title>Timer</title>
</head>
<body>


	<div id="main">
	<%@include file="header.jsp" %>
		<%
currentUser = new User();
currentUser = (User) session.getAttribute("user");
if(session.getAttribute("user")==null){
		response.sendRedirect("login.jsp");
			}
else if (currentUser.getRole().equals(User.Role.TUTOR)){
	%>
	<div id=error>
		<p class="warning">
			As a tutor you can not track activities.<br>
		</p>
	</div>
	<%
		}
			
 else{
		User currentUser1 = (User) session.getAttribute("user");
		currentUser = UserRegistry.getInstance().getUserById(currentUser1.getId());
		List<String> errors = (List<String>) session.getAttribute("errors");
		if (errors != null && !errors.isEmpty()) {
					%>

					<div id=error>
					<p class="warning">
					An error occurred:
					<%
					for(String error: errors){
						%><%=error%><br>
						<%
					}
					%>
					</p>
					</div>
		<%
		}
		if (request.getParameter("t") == null) {
				
				if(request.getParameter("bevestig")==null){
					if(currentUser.getTimerStatus()==true){
						response.sendRedirect("timer.jsp?t=started");
					}
					
				%>
				<fieldset>
				<legend>Start Timer</legend>
				<form id='editForm' name="startCheck" action="timer.jsp" method="get">

				<%
					currentUser = (User) session.getAttribute("user");
					List<Long> courses = currentUser.getCourses();
					if(!courses.isEmpty()){
						%>
						<select name="vak" style="width: 100%; text-align: center; height: 25px; font-size: 20px;">
						<%
						for(long courseId : courses){
							String courseName = CourseRegistry.getInstance().getCourseName(courseId);
							%>
							<option><%=courseName%></option>
							<%
						}
						%>
						</select>
						<br />
						<br /> <select name="type" style="width: 100%; text-align: center; height: 25px; font-size: 20px;">
						<option>Studying</option>
						<option>Worksession</option>
						<option>Lecture</option>
						<option>Other</option>
						</select> <br />  <br />
						<div class="button">
						<input type="submit" value="Confirm" name="bevestig" style="width: 100%;">
						</div>
						<%
					}
					else{
						%>
						You have not yet selected any courses!<%
						}
						%>
				</form>
				</fieldset>
				<%
			}
			else{
				String vak = request.getParameter("vak");
				String type = request.getParameter("type");
				%>
				<fieldset>
				<legend>Confirm Timer</legend>
				Start timing for <b><%=type%></b> of the course <b><%=vak%></b>?.
				<br>
				<br>
				<form name="startTimer2" action="Time" method="post">
				<input type="hidden" value="<%=type%>" name="type"> 
				<input type="hidden" value="<%=vak%>" name="vak">
				<div class="button">
				<input type="submit" value="Start timer" name="start" style="width: 100%;">
				</div>
				</form>
				<form name="wijzigInstellingen" method="post" action="timer.jsp">
				<div class="button">
				<br>
				<input type="submit" value="Change Value" name="wijzig" style="width: 100%;"></div></form>
				
				</fieldset>
				<%
			}
		} 
		else if (request.getParameter("t").equals("started") && currentUser.getTimerStatus()==true) {
			response.sendRedirect("dashboard.jsp?I=started");
			Activity currentActivity = currentUser.getActivities().get(currentUser.getActivities().size()-1);
			if(session.getAttribute("stop")!=null){session.removeAttribute("stop");}
			%>
			<fieldset>
			<form name="d">
			<input type="text" size="15" name="d2" disabled="disabled">
			<input type="hidden" name="">
			</form>
		<% Date now = new Date();
			long timeLapseS=(now.getTime()-currentActivity.getStart().getTime())/1000;
			long timeLapseM=timeLapseS/60;
			long hours=timeLapseM/60;
			long minutes=timeLapseM % 60;
			long seconds= timeLapseS % 60;
			%>
		
			Aan het timen sinds:
			<b>
				<%=currentActivity.getStart()%></b>
			
			voor een 
			<b>
				
				<%=currentActivity.getType()%>
			</b>
			van het vak 
			<b>
				
				<%=CourseRegistry.getInstance().getCourseName(currentActivity.getCourseId())%>
			</b>
			</fieldset>
			<script>
			<!--
			/*By George Chiang. (JK's ultimate JavaScript tutorial and free JavaScripts site!)
			http://www.javascriptkit.com
			Credit MUST stay intact for use*/
			var milisec = 0
			var seconds = <%=seconds%>;
			var minutes = <%=minutes%>;
			var hours = <%=hours%>;
			
			document.d.d2.value = '0'

			function display() {
				if (milisec >= 9) {
					milisec = 0
					seconds += 1
				} 
				
					if(seconds>=59){
						seconds=0
						minutes+=1
					}
					if(minutes>=59){
						minutes=0
						hours+=1
					}
				else
					milisec += 1
				document.d.d2.value = hours+ "h "+ minutes+" min "+seconds + " sec"

				setTimeout("display()", 100)
			}
			display()
			//-->
			</script>
			<fieldset>
			<form method="post" name="stopTimer" action="timer.jsp?t=stop">
			<div class="button">
			<input type="submit" value="Stop the timer" name="stopTimer">
			</div>
			</form>
			</fieldset>
			<%
		}
		else if (request.getParameter("t").equals("stop") && currentUser.getTimerStatus()==true && !currentUser.getActivities().isEmpty()) {
		%>
		<fieldset>
		<b><i>Are you sure you want to stop or do you want to </i></b><br>
				<div class="button">
			<form method="get" action="timer.jsp">
			<input type="hidden" name="t" value="started">
			<input type="submit" value="Continue the activitity" ></form></div>
		<form name="finishTimer" method="post" action="Time" style="font-size: 25px;">

				<%
		Date start = currentUser.getActivities().get(currentUser.getActivities().size()-1).getStart();
		Date stop = new Date();
		
		
		if (session.getAttribute("stop") != null) {
			stop = (Date) session.getAttribute("stop");
		}
		else {
			session.setAttribute("stop", stop);
		}
		%>
	<%
							String location="";
							if(session.getAttribute("location")!=null){
							location = (String) session.getAttribute("location");
							}
							long timeLapseMs = stop.getTime() - start.getTime();
							long timeLapseS = timeLapseMs / 1000;
							long timeLapseM = timeLapseS / 60;
							long seconds = timeLapseS % 60;
							long hours = timeLapseM / 60;
							long minutes = timeLapseM % 60;
							
				%>
				 Hours <br /> 
				<input style="font-size: 25px; height: 33px; width: 100%;" size="10" type="text" name="hours"	value="<%=hours%>"> <br /> Minutes <br />
				<input  style="font-size: 25px; height: 33px; width: 100%;"size="10" type="text" name="minutes" value="<%=minutes%>"> <br /> Seconds <br />
				<input	 style="font-size: 25px; height: 33px; width: 100%;" type="text" size="10" name="seconds" value="<%=seconds%>">
				<font size="5" color="lightblue"> (It's only possible to lower the time.)</font>
				<br /> <br /> Location : <br><input  style="font-size: 25px; height: 33px; width: 100%;" value="<%=location%>" type="text"	name="location"><br> 
				 <br> Appreciation: <br> 
				 <input checked=true type="radio" name="appreciation" value=1>1
				 <input type="radio" name="appreciation" value=2>2
				 <input type="radio" name="appreciation" value=3>3
				 <input type="radio" name="appreciation" value=4>4
				 <input type="radio" name="appreciation" value=5>5
				 <input type="radio" name="appreciation" value=6>6
				 <input type="radio" name="appreciation" value=7>7
				 <input type="radio" name="appreciation" value=8>8
				 <input type="radio" name="appreciation" value=9>9
				 <input type="radio" name="appreciation" value=10>10<br>
				 

				<%
					
					String type = currentUser.getActivities().get(currentUser.getActivities().size()-1).getType();
							if (type.equals("Studying")) {
				%>

				<br> Number of pages : <br> <input style="font-size: 25px; height: 33px; width: 100%;" type="text" name="nrOfPages"><br>
				<%
					}
				%>
				<br />
				<div class="button">
				<input style="width: 100%;" type="submit" value="Stop" name="addActivity">
				</div>
			</form>
			
		</fieldset>
		<%
		}
		else if (currentUser.getTimerStatus()==false && request.getParameter("t").equals("stopped")) {
		session.setAttribute("fbCaption","I just studied "+ session.getAttribute("totalTime") +" for "+ session.getAttribute("timerCourse"));
		session.setAttribute("fbName", "Just finished studying!");
		%>
		<br> Your activity was successfully added.<br> <a href="timer.jsp">Create new activity</a> or share on facebook/twitter: <%@include file="facebook.jsp" %>
		<%
		session.removeAttribute("timerType");
		session.removeAttribute("stop");
		session.removeAttribute("time");
		session.removeAttribute("totalTime");
		session.removeAttribute("timerCourse");
		}
		else {
		%>
		<%response.sendRedirect("timer.jsp");%>

		<%
		}
		session.removeAttribute("errors");
	}
%>
</div>
</body>
</html>